package cn.itlight.basic.controller;

import cn.itlight.basic.exception.BusinessException;
import cn.itlight.basic.service.IVerifyCodeService;
import cn.itlight.basic.utils.AjaxResult;
import cn.itlight.basic.vo.EmailRegisterVo;
import cn.itlight.basic.vo.LoginVo;
import cn.itlight.basic.vo.RegisterVo;
import cn.itlight.basic.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/verifyCode")
/**
* @Description: 用户注册控制层
* @Author: TomLiu
* @Version: 1.0
* @Date:  2022/7/7 8:40
*/
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /*
    * @Title: upload
    * @Description: 页面加载获取图形验证码
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/7/7 8:39
    * @Parameters:
    * @Return
    */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult upload(@PathVariable("imageCodeKey") String imageCodeKey){
        try {
            String imageBase64Str= verifyCodeService.getImageCode(imageCodeKey);
            // 3.返回成功信息+文件访问路径
            return AjaxResult.me().setResultObj(imageBase64Str);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /*
    * @Title: upload
    * @Description: TODO
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/7/9 16:54
    * @Parameters:
    * @Return
    */
    @GetMapping("/imageEmail/{imageCodeKeyEmail}")
    public AjaxResult email(@PathVariable("imageCodeKeyEmail") String imageCodeKeyEmail){
        try {
            String imageBase64Str= verifyCodeService.getImageCode(imageCodeKeyEmail);
            // 3.返回成功信息+文件访问路径
            return AjaxResult.me().setResultObj(imageBase64Str);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /*
    * @Title: smsCode
    * @Description: 获取手机短信验证码
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/7/7 8:40
    * @Parameters:
    * @Return
    */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterVo registerVo) {
        try {
            // 调用业务层发短信验证码
            verifyCodeService.smsCode(registerVo);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping("/emailCode")
    public AjaxResult emailCode(@RequestBody EmailRegisterVo emailRegisterVo) {
        try {
            // 调用业务层发短信验证码
            verifyCodeService.emailCode(emailRegisterVo);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginVo loginVo) {
        System.out.println(loginVo.getUsername());
        try {
            // 调用业务层发短信验证码
            verifyCodeService.loginCode(loginVo);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping("/shop")
    public AjaxResult shop(@RequestBody ShopVo shopVo) {
        try {
            // 调用业务层发短信验证码
            verifyCodeService.shopCode(shopVo);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}
